package hard

import (
	"sort"
)

func MinOperations(nums []int) int {
	n := len(nums)
	set := make(map[int]bool)
	for _, num := range nums {
		set[num] = true
	}
	sortedUniqueNums := []int{}
	for num, _ := range set {
		sortedUniqueNums = append(sortedUniqueNums, num)
	}
	sort.Ints(sortedUniqueNums)
	res := n
	j := 0
	for i, left := range sortedUniqueNums {
		right := left + n - 1
		for j < len(sortedUniqueNums) && sortedUniqueNums[j] <= right {
			res = min(res, n-(j-i+1))
			j++
		}
	}
	return res
}
